package com.hk.userservice.service.impl;

import com.hk.model.WxRolePermissionRelation;
import com.hk.userservice.mapper.WxRolePermissionRelationMapper;
import com.hk.userservice.service.WxRolePermissionRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/26 10:43
 * @Statement: 角色权限接口实现类
 */
@Slf4j
@Service
public class WxRolePermissionRelationServiceImpl implements WxRolePermissionRelationService {

    @Autowired
    private WxRolePermissionRelationMapper rolePermissionRelationMapper;

    @Override
    public List<WxRolePermissionRelation> findAll(Integer page, Integer limit, WxRolePermissionRelation wxRolePermissionRelation) {
        return null;
    }

    @Override
    public int count(Integer page, Integer limit, WxRolePermissionRelation wxRolePermissionRelation) {
        return 0;
    }

    @Override
    public WxRolePermissionRelation findById(Integer id) {
        return null;
    }


    @Override
    public List<WxRolePermissionRelation> findByRoleId(Integer id) {
        List<WxRolePermissionRelation> rolePermissionRelationList =  rolePermissionRelationMapper.findByRoleId(id);
        return rolePermissionRelationList;
    }

    @Override
    public Set<Integer> findByRoleIds(List<Integer> roleIds) {
        Set<Integer> permissionIds = rolePermissionRelationMapper.findByRoleIds(roleIds);
        return permissionIds;
    }

    @Transactional
    @Override
    public int save(WxRolePermissionRelation wxRolePermissionRelation) {
        //        1、先删除
        rolePermissionRelationMapper.deleteByRoleId(wxRolePermissionRelation.getRoleId());
//        2、遍历添加
        String[] split = wxRolePermissionRelation.getPermissionIds().split(",");
        int count = 0;
        for (int i = 0; i < split.length; i++) {
            WxRolePermissionRelation rolePermissionRelation = WxRolePermissionRelation.builder().roleId(wxRolePermissionRelation.getRoleId())
                    .permissionId(Integer.parseInt(split[i])).build();
            int save = rolePermissionRelationMapper.save(rolePermissionRelation);
            count +=save;
        }
        if (count > 0) {
            log.error("角色权限修改成功！");
        }
        return 0;

    }

    @Override
    public int update(WxRolePermissionRelation wxRolePermissionRelation) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int deleteByRoleId(Integer id) {
        int delete = rolePermissionRelationMapper.deleteByRoleId(id);
        return delete;
    }
}
